#coding=utf-8

import webbrowser
import Tkinter
from base_client import base_client

def predicate_loginform( form ):
    if form.attrs.has_key("id") and form.attrs["id"]=="frm_login":
        return True
    return False

def predicate_blogform( form ):
    if form.attrs.has_key("id") and form.attrs["id"]=="form_blog":
        return True
    return False

def predicate_forumform( form ):
    if form.attrs.has_key("id") and form.attrs["id"]=="form_thread":
        return True
    return False

class client(base_client):
    def __init__(self, config=None ):
        if None==config:
            config = { 
                      "blog_user":"", 
                      "blog_password":"", 
                      "forum_user":"", 
                      "forum_password":"", 
                      "forum":"技术开发交流",
                      "type":"普通话题",
                      "email_when_new_post":1
                      }
        base_client.__init__( self, config )

    def __blog_login(self):
        self.browser.open( 'http://www.oschina.net/home/login' )
        self.browser.select_form( predicate=predicate_loginform )
        self.browser["email"] = self.var_blog_user.get()
        self.browser["pwd"] = self.var_blog_password.get()
        self.browser["save_login"] = ["1"]
        self.browser.submit()

    def __forum_login(self):
        self.browser.open( 'http://www.oschina.net/home/login' )
        self.browser.select_form( predicate=predicate_loginform )
        self.browser["email"] = self.var_forum_user.get()
        self.browser["pwd"] = self.var_forum_password.get()
        self.browser["save_login"] = ["1"]
        self.browser.submit()

    # not ready
    def post_blog(self, title, content ):
        self.btn_blog_state.config( bg="yellow" )
        try:
            self.__blog_login()
            self.browser.open( 'http://my.oschina.net/home/go?page=blog' )
            self.browser.follow_link( text_regex=r"http://my.oschina.net/*/admin/new-blog" )
            self.browser.select_form( predicate=predicate_blogform )
            self.browser["title"] = title
            self.browser["content"] = content
            self.browser.submit()
        except:
            self.btn_blog_state.config( bg="red" )
        else:
            self.btn_blog_state.config( bg="blue" )

    def post_topic(self, title, content ):
        self.btn_forum_state.config( bg="yellow" )
        try:
            self.__forum_login()
            self.browser.open( 'http://www.oschina.net/bbs/new_thread' )
            self.browser.select_form( predicate=predicate_forumform )
            self.browser["title"] = title
            self.browser["content"] = content
            
            if "技术开发交流"==self.var_forum.get():
                self.browser["forum"] = ["1"]
            elif "软件使用交流"==self.var_forum.get():
                self.browser["forum"] = ["5"]
            elif "IT大杂烩"==self.var_forum.get():
                self.browser["forum"] = ["2"]
            elif "招聘与求职"==self.var_forum.get():
                self.browser["forum"] = ["4"]
            else:
                self.browser["forum"] = ["3"]

            if "普通话题"==self.var_type.get():
                self.browser["type"] = ["1"]
            elif "转帖"==self.var_type.get():
                self.browser["type"] = ["2"]
            else:
                self.browser["type"] = ["3"]
            
            if 1==self.var_email_me.get():
                self.browser["email_when_new_post"] = ["1"]

            self.browser.submit()
        except:
            self.btn_forum_state.config( bg="red" )
        else:
            self.btn_forum_state.config( bg="blue" )

    def display_blog_option(self, father):
        base_client.display_blog_option( self, father, "开源中国", self.call_browser )

    def display_forum_option(self, father):
        base_client.display_forum_option( self, father, "开源中国", self.call_browser )
        
        self.var_forum = Tkinter.StringVar()
        self.var_forum.set( self.config["forum"] )
        Tkinter.OptionMenu( father, self.var_forum,"技术开发交流","软件使用交流","IT大杂烩","招聘与求职","意见建议和站务" ).grid( row=0, column=6, sticky=Tkinter.W )
        
        self.var_type = Tkinter.StringVar()
        self.var_type.set( self.config["type"] )
        Tkinter.OptionMenu( father, self.var_type,"普通话题","转帖","提问" ).grid( row=0, column=7, sticky=Tkinter.W )

        self.var_email_me = Tkinter.IntVar()
        self.var_email_me.set( self.config["email_when_new_post"] )
        Tkinter.Checkbutton( father, text="有回帖时邮件通知我", variable=self.var_email_me ).grid( row=0, column=8, sticky=Tkinter.W )

    def call_browser(self):
        webbrowser.open_new_tab('http://www.oschina.net/home/go?page=blog')
        
    def update_config(self):
        if "var_blog_user" in dir(self):
            self.config["blog_user"] = self.var_blog_user.get()
            self.config["blog_password"] = self.var_blog_password.get()
        if "var_forum_user" in dir(self):
            self.config["forum_user"] = self.var_forum_user.get()
            self.config["forum_password"] = self.var_forum_password.get()
            self.config["forum"] = self.var_forum.get()
            self.config["type"] = self.var_type.get()
            self.config["email_when_new_post"] = self.var_email_me.get()